/*
 * @Description: Echarts图表模型js
 * @Author: Huang Junjie
 * @Date: 2021-06-03 11:24:09
 * @LastEditTime: 2021-07-06 09:44:46
 * @LastEditors: Liu Zhiwang
 */
// 引入echarts
// const echarts = require('echarts')

// 当月检测缺陷数统计 - 饼状图
export function PieChart01(myChart) {
  myChart.clear();
  myChart.setOption({
    tooltip: {
      trigger: 'item'
    },
    legend: {
      bottom: 0,
      left: 20,
      itemWidth: 18,
      itemHeight: 10,
      itemGap: 10,
      orient: 'vertical',
      textStyle: {
        color: '#ffffff',
        fontSize: '14px'
      },
      formatter: function(name) {
        // 获取legend显示内容
        const data = [
          { value: 578, name: '一级缺陷', itemStyle: { color: '#eaad1a' }},
          { value: 1016, name: '二级A类缺陷', itemStyle: { color: '#02c2bb' }},
          { value: 3582, name: '二级B类缺陷', itemStyle: { color: '#3695ff' }}
        ];
        // let total = 0
        let tarValue = 0;
        for (let i = 0, l = data.length; i < l; i++) {
          // total += data[i].value
          if (data[i].name === name) {
            tarValue = data[i].value;
          }
        }
        // const p = (tarValue / total * 100).toFixed(2)
        return name + tarValue + '个';
      }
    },
    series: [
      {
        clockwise: true,
        center: ['50%', '40%'],
        name: '当月检测缺陷数统计',
        type: 'pie',
        radius: ['0%', '60%'],
        avoidLabelOverlap: true,
        label: {
          normal: {
            show: true,
            position: 'inner',
            textStyle: {
              fontSize: '14px',
              color: '#0c265f'
            },
            formatter: '{d}%'
          }
        },
        labelLine: {
          show: true
        },
        tooltip: {
          show: true,
          trigger: 'item',
          formatter: '{a} <br/>{b}: {c} ({d}%)',
          position: ['0%', '50%']
        },
        data: [
          { value: 578, name: '一级缺陷', itemStyle: { color: '#eaad1a' }},
          { value: 1016, name: '二级A类缺陷', itemStyle: { color: '#02c2bb' }},
          { value: 3582, name: '二级B类缺陷', itemStyle: { color: '#3695ff' }}
        ]
      }
    ]
  });

  // 解决自适应
  setTimeout(function() {
    window.addEventListener('resize', () => {
      myChart.resize();
    });
  }, 500);
}
